package com.eryos.android.cigarettecounter.tool;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.eryos.android.cigarettecounter.R;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

/**
 * Project : CigaretteCounter Author : Olivier Combe URL : http://eryos.fr/CigaretteCounter/
 * 
 * Copyright(C)2011 - Olivier Combe
 * 
 * Licensed under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * 
 * @version \$Revision.*\$
 * @author Olivier Combe
 */
public class GoogleAnalyticsProvider {

    private static final String GOOGLE_ANALYTICS_TRACKER_ID = "UA-8541799-7";

    /** Scope level for Visitor. */
    public static final int SCOPE_VISITOR_LEVEL = 1;
    /** Scope level for Session. */
    public static final int SCOPE_SESSION_LEVEL = 2;
    /** Scope level for Page. */
    public static final int SCOPE_PAGE_LEVEL = 3;

    private static final String APP_VERSION_TAG = "App_version";
    private static final String ANDROID_VERSION_TAG = "Android_version";
    private static final String SDK_TAG = "Android_sdk";
    private static final String BRAND_TAG = "Brand";
    private static final String DEVICE_TAG = "Device";
    private static final String APP_BUILD_TAG = "Build";

    private static boolean trackerStarted = false;
    private static GoogleAnalyticsTracker tracker;
    private static Context context;

    /**
     * Start a new tracker or retrieve the last one used.
     * 
     * @param contextArg
     *            : application context
     */
    public static void startTracker(final Context contextArg) {
        if (tracker == null) {
            context = contextArg;
            tracker = GoogleAnalyticsTracker.getInstance();
            Log.d("CigaretteCounter", "GoogleAnalytics : Tracker initialized ...");
        }
        if (!isTrackerStarted()) {
            tracker.start(GOOGLE_ANALYTICS_TRACKER_ID, context);
            trackerStarted = true;
            Log.d("CigaretteCounter", "GoogleAnalytics : Tracker started ...");
        }
    }

    /**
     * Stop tracker and dispatch data to Analytics Service.
     */
    public static void stopTracker() {
        if (isTrackerValid()) {
            tracker.dispatch();
            tracker.stop();
            trackerStarted = false;
            Log.d("CigaretteCounter", "GoogleAnalytics : Tracker stoped");
        }
    }

    /**
     * Track the page displayed.
     * 
     * @param page
     *            to track
     */
    public static void trackPage(final String page) {
        Log.d("CigaretteCounter", "GoogleAnalytics : trackPage : " + page);
        if (isTrackerValid()) {
            tracker.trackPageView(page);
        }
    }

    /**
     * Dispatch tracker information to Google system.
     */
    public static void dispatch() {
        Log.d("CigaretteCounter", "GoogleAnalytics : dispatch");
        if (isTrackerValid()) {
            tracker.dispatch();
        }
    }

    /**
     * Register custom VAR in analytics system.
     */
    public static void trackCustomVars() {
        if (isTrackerValid()) {

            // Retrieve Device and Software informations
            // PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            String appVersion = context.getString(R.string.version); // packageInfo.versionName;
            String buildVersion = context.getString(R.string.build); // packageInfo.versionName;
            String androidVersion = Build.VERSION.RELEASE;
            String sdk = Build.VERSION.SDK;
            String brand = Build.MANUFACTURER;
            String device = Build.MANUFACTURER + " : " + Build.MODEL;

            /*
             * setCustomVar(SCOPE_VISITOR_LEVEL, APP_VERSION_TAG, appVersion); setCustomVar(SCOPE_VISITOR_LEVEL,
             * APP_BUILD_TAG, buildVersion); setCustomVar(SCOPE_VISITOR_LEVEL, ANDROID_VERSION_TAG, androidVersion);
             * setCustomVar(SCOPE_VISITOR_LEVEL, SDK_TAG, sdk); setCustomVar(SCOPE_VISITOR_LEVEL, BRAND_TAG, brand);
             * setCustomVar(SCOPE_VISITOR_LEVEL, DEVICE_TAG, device);
             */

            trackEvent("Apps_Infos", APP_VERSION_TAG, appVersion);
            trackEvent("Apps_Infos", APP_BUILD_TAG, buildVersion);
            trackEvent("OS", ANDROID_VERSION_TAG, androidVersion);
            trackEvent("OS", SDK_TAG, sdk);
            trackEvent("Device", BRAND_TAG, brand);
            trackEvent("Device", DEVICE_TAG, device);

        }
    }

    /**
     * Define Custom Var.
     * 
     * @param scope
     *            : default is 1
     * @param tag
     *            : Var Name
     * @param value
     *            : Var Value
     * @return true if recorded
     */
    public static boolean setCustomVar(final int scope, final String tag, final String value) {
        if (isTrackerValid()) {
            boolean res = tracker.setCustomVar(1, tag, value, 2);
            Log.d("CigaretteCounter", "GoogleAnalytics : Track : " + tag + " : " + res);
            return res;
        } else {
            return false;
        }
    }

    /**
     * Check if the tracker is strated or not.
     * 
     * @return true if the tracker is started
     */
    public static boolean isTrackerStarted() {
        return trackerStarted;
    }

    /**
     * Check is the tracer is valid and started.
     * 
     * @return true if tracked is valid
     */
    public static boolean isTrackerValid() {
        if (tracker != null && isTrackerStarted()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Track a specific event.
     * 
     * @param category
     *            of the event
     * @param eventName
     *            of the event
     * @param eventValue
     *            of the event
     * @return if the event is correctly tracked
     */
    public static boolean trackEvent(final String category, final String eventName, final String eventValue) {
        tracker.trackEvent(category, eventName, eventValue, 1);
        return true;
    }

    /**
     * Track a specific event.
     * 
     * @param eventName
     *            of the event
     * @param eventValue
     *            of the event
     * @return if the event is correctly tracked
     */
    public static boolean trackEvent(final String eventName, final String eventValue) {
        tracker.trackEvent("Click", eventName, eventValue, 1);
        return true;
    }
}
